package com.zhiyou100.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyou100.springbootmybatisplus.common.QueryPageParam;
import com.zhiyou100.springbootmybatisplus.common.Result;
import com.zhiyou100.springbootmybatisplus.pojo.Goods;
import com.zhiyou100.springbootmybatisplus.pojo.Record;
import com.zhiyou100.springbootmybatisplus.service.GoodsService;
import com.zhiyou100.springbootmybatisplus.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YourName
 * @since 2025-02-13
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService service;
    @GetMapping("/list")
    public List<Record> list() {
        return service.list();
    }

    @PostMapping("/save")
    public Result save(@RequestBody Record record) {
        return service.save(record) ? Result.suc() : Result.fail();
    }

//    @GetMapping("/findByNo")
//    public Result findByNo(@RequestParam String no) {
//        // 调用 .list() 执行查询并返回 List
//        List<Goods> list = service.lambdaQuery().eq(Goods::getNo, no).list();
//        // 判断结果是否为空，并返回相应的结果
//        return list.size() > 0 ? Result.suc(list) : Result.fail();
//    }

    @GetMapping("/del")
    public Result del(@RequestParam String id) {
        return service.removeById(id) ? Result.suc() : Result.fail();
    }


    @PostMapping("/update")
    public Result update(@RequestBody Record record) {
        return service.updateById(record) ? Result.suc() : Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String goodstype = (String) param.get("goodstype");
        String storage = (String) param.get("storage");
        Page<Record> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Record> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
//            lambdaQueryWrapper.like(Record::getName, name);
        }
        if (StringUtils.isNotBlank(goodstype) && !"null".equals(goodstype)) {
//            lambdaQueryWrapper.like(Record::getName, name);
        }
        if (StringUtils.isNotBlank(storage) && !"null".equals(storage)) {
//            lambdaQueryWrapper.like(Record::getName, name);
        }
//        IPage result = userService.pageC(page);
        IPage result = service.pageCC(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }
}
